
/********************本地服务器*************************/
/** API请求地址 */
var httpURL='http://localhost:9090/demo-api/';

//金钱相关
var AVAILABLE_TOO_SMALL = '可用余额不足！';
var PROJECT_TOO_SMALL = '投资金额不能超过可投金额！！';
var INSUFFICIENT_FREEZING_AMOUNT ="投资金额大于用户冻结资金!";
var SAME_INVESTMENT_OBJECT = "当前投资账户与债权转让账户或项目（标）借款账户相同，不能进行投资！";
var NON_TRANSFER_STATE = "该债权转让状态不在转让中，不能进行转让投资！";
var AMOUNT_CANNOT_BE_EMPTY = "承接金额不能为空！";
var MONEY_FORMAT_ERROR = "金额必须为大于0的数字，且最多保留两位小数！";
var TRANSFER_AMOUNT_TOO_LARGE = "转让投资金额不能超过可投金额！";
var INVEST_AMOUNT = "投资金额不能为空！";
var  NOT_ACCEPTED_LOAN = "您还未接受借款协议!";
var LOWER_LIMIT = '起投金额至少为1000元！';
var UPPER_LIMIT_INDIVIDUAL_INVESTMENT = '个人投资金额不可超过20万元!';
var TOTAOL_MUST_GREATER = '投资工具总金额必须要大于已投金额！';
//资料未完善相关
var CANNOT_BE_NULL='有必填选项不能为空!';
var INCOMPLETE_DATA = '您尚未完善资料，请完善后再投资！';
var AUTHENTICATION = '实名认证中，暂不能投资！';
var AUTHENTICATION_FAILURE = '实名认证失败，请重新认证！';
var INCOMPLETE_RISK_ASSESSMENT = '您尚未完成风险测评，请完成后投资！';
var REEVALUATION = '根据风险测评，您的风险承受能力为：风险厌恶型；若想继续投资，请重新测评！';
//请求返回数据
var INVEST_SUCCESS = '投资成功!';
var FAILED_INVEST = '投资失败！';
var SAME_PERSON = "转让人和承接人不能是同一人!";
var SUCCESSFUL_APPLICATION = '申请成功！';
var NOT_ABOLISH = '已生效工具不得作废!';
var SYSTEM_ERROR = '系统错误！';

function checkIsNull(name) {
    if(name==null||name==undefined||name=="")
        return false;
    else
        return true;
}

/**
 * 判断是否登录
 * @returns {boolean}
 */
function isLogin() {
    var val = sessionStorage.getItem('token');
    if(val == null || val == undefined || val==""){
        return false;
    }
    return true;
}

/**
 * 判断是否登录失效
 * @returns {*}
 */
function isLoginLose(){
    return ajaxHelper().getTokenVerification(ajaxHelper().getTokenExp(sessionStorage.getItem("token")));
}


//提示框的封装
(function ($) {

    window.Ewin = function () {
        var html = '<div id="[Id]" class="modal fade" role="dialog" aria-labelledby="modalLabel">' +
            '<div class="modal-dialog modal-sm">' +
            '<div class="modal-content">' +
            '<div class="modal-header">' +
            '<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>' +
            '<h4 class="modal-title" id="modalLabel">[Title]</h4>' +
            '</div>' +
            '<div class="modal-body">' +
            '<p>[Message]</p>' +
            '</div>' +
            '<div class="modal-footer">' +
            '<button type="button" class="btn btn-default cancel" data-dismiss="modal">[BtnCancel]</button>' +
            '<button type="button" class="btn btn-primary ok" data-dismiss="modal">[BtnOk]</button>' +
            '</div>' +
            '</div>' +
            '</div>' +
            '</div>';


        var dialogdHtml = '<div id="[Id]" class="modal fade" role="dialog" aria-labelledby="modalLabel">' +
            '<div class="modal-dialog">' +
            '<div class="modal-content">' +
            '<div class="modal-header">' +
            '<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>' +
            '<h4 class="modal-title" id="modalLabel">[Title]</h4>' +
            '</div>' +
            '<div class="modal-body">' +
            '</div>' +
            '</div>' +
            '</div>' +
            '</div>';
        var reg = new RegExp("\\[([^\\[\\]]*?)\\]", 'igm');
        var generateId = function () {
            var date = new Date();
            return 'mdl' + date.valueOf();
        }
        var init = function (options) {
            options = $.extend({}, {
                title: "操作提示",
                message: "提示内容",
                btnok: "确定",
                btncl: "取消",
                width: 200,
                auto: false
            }, options || {});
            var modalId = generateId();
            var content = html.replace(reg, function (node, key) {
                return {
                    Id: modalId,
                    Title: options.title,
                    Message: options.message,
                    BtnOk: options.btnok,
                    BtnCancel: options.btncl
                }[key];
            });
            $('body').append(content);
            $('#' + modalId).modal({
                width: options.width,
                backdrop: 'static'
            });
            $('#' + modalId).on('hide.bs.modal', function (e) {
                $('body').find('#' + modalId).remove();
            });
            return modalId;
        }

        return {
            alert: function (options) {
                if (typeof options == 'string') {
                    options = {
                        message: options
                    };
                }
                var id = init(options);
                var modal = $('#' + id);
                modal.find('.ok').removeClass('btn-success').addClass('btn-primary');
                modal.find('.cancel').hide();

                return {
                    id: id,
                    on: function (callback) {
                        if (callback && callback instanceof Function) {
                            modal.find('.ok').click(function () { callback(true); });
                        }
                    },
                    hide: function (callback) {
                        if (callback && callback instanceof Function) {
                            modal.on('hide.bs.modal', function (e) {
                                callback(e);
                            });
                        }
                    }
                };
            },
            confirm: function (options) {
                var id = init(options);
                var modal = $('#' + id);
                modal.find('.ok').removeClass('btn-primary').addClass('btn-success');
                modal.find('.cancel').show();
                return {
                    id: id,
                    on: function (callback) {
                        if (callback && callback instanceof Function) {
                            modal.find('.ok').click(function () { callback(true); });
                            modal.find('.cancel').click(function () { callback(false); });
                        }
                    },
                    hide: function (callback) {
                        if (callback && callback instanceof Function) {
                            modal.on('hide.bs.modal', function (e) {
                                callback(e);
                            });
                        }
                    }
                };
            },
            dialog: function (options) {
                options = $.extend({}, {
                    title: 'title',
                    url: '',
                    width: 800,
                    height: 550,
                    onReady: function () { },
                    onShown: function (e) { }
                }, options || {});
                var modalId = generateId();

                var content = dialogdHtml.replace(reg, function (node, key) {
                    return {
                        Id: modalId,
                        Title: options.title
                    }[key];
                });
                $('body').append(content);
                var target = $('#' + modalId);
                target.find('.modal-body').load(options.url);
                if (options.onReady())
                    options.onReady.call(target);
                target.modal();
                target.on('shown.bs.modal', function (e) {
                    if (options.onReady(e))
                        options.onReady.call(target, e);
                });
                target.on('hide.bs.modal', function (e) {
                    $('body').find(target).remove();
                });
            }
        }
    }();
})(jQuery);

/**
 * 日期格式化方法
 * @param format
 * @returns {*}
 */
Date.prototype.format = function(

) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(), //day
        "h+": this.getHours(), //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if(/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    }

    for(var k in o) {
        if(new RegExp("("+ k +")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
        }
    }
    return format;
}

/**
 * 验证身份信息
 * @returns {boolean}
 */
function isLoginTool(){
    if(isLogin()){
        if(!isLoginLose()){
            return true;
        }else{
            Ewin.alert('登录过期，请重新登录！').on(function(){
                sessionStorage.clear();
                window.location.href="login.html?"+(new Date()).getTime();
            });
        }
    }else{
        Ewin.alert('未登录，请先登录！').on(function(){
            window.location.href="login.html?"+(new Date()).getTime();
        });
    }
}

/**
 * 验证金钱正则表达式
 * @param value
 */
function moneyRegular(value) {
    var reg =  /^(([1-9]\d*))(\.\d{1,2})?$/;
    if(reg.test(value)){
        return true;
    }
    return false;
}
/**
 * 注册组件
 * @param val 组件名
 */
function certifiedComponents(val){
    if(val.length!=0){
        for(i=0;i<val.length;i++){
            Vue.component(val[i], {template: '<div></div>'});
        }
    }
}